
package com.the9tcat.deliverycoming;

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.AlertDialog;
import android.app.PendingIntent;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.view.KeyEvent;
import android.widget.Toast;

import com.mobclick.android.MobclickAgent;
import com.the9tcat.deliverycoming.data.Configure;
import com.the9tcat.deliverycoming.db.DeliveryDb;
import com.the9tcat.deliverycoming.modle.DeliveryInfo;
import com.the9tcat.deliverycoming.net.NetworkCheck;

/**
 * The Class SplashActivity.
 */
public class SplashActivity extends Activity {

    /** The SPLAS h_ time. */
    private final long SPLASH_TIME = 2000L;

    private AlarmManager mAlarm;

    /*
     * (non-Javadoc)
     * @see android.app.Activity#onCreate(android.os.Bundle)
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	MobclickAgent.setUpdateOnlyWifi(false);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);
        MobclickAgent.onError(this);
        // for test: please remove it in release version    
        //setTestData();
        startMsgService();

    }

    protected void setTestData() {
        //DeliveryDb db = new DeliveryDb(this);
        DeliveryDb db = DeliveryDb.getDb(this);
        //db.open();
        if (db.getDeliveryInTrack(Configure.MODE_ALL_TRACKLIST).isEmpty()) {
            DeliveryInfo delivery = new DeliveryInfo();
            delivery.setName("test delivery 1");
            delivery.setExpressNu("1467863290");
            delivery.setCompanyId("yuantong");
            delivery.setExpComName("圆通速递");
            db.add(delivery);
            DeliveryInfo delivery1 = new DeliveryInfo();
            delivery1.setName("test delivery 2");
            delivery1.setExpressNu("106762869048");
            delivery1.setCompanyId("shunfeng");
            delivery1.setExpComName("顺丰速递");
            db.add(delivery1);
           
        }

       // db.close();

    }

    private void startMsgService() {
        mAlarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        PendingIntent startIntent = PendingIntent
                .getBroadcast(this, 0, new Intent(AlarmReceiver.START_ALARM), 0);

        PendingIntent cancleIntent = PendingIntent
                .getBroadcast(this, 0, new Intent(AlarmReceiver.CANCLE_ALARM), 0);
        Configure configuer = Configure.getInstance(this);
        if (configuer.isDndEnabled()) {
            Date date = new Date();
            if (date.getHours() > configuer.getDndStart()
                    && date.getHours() < configuer.getDndEnd()) {
                mAlarm.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),
                           startIntent);
            }
            date.setHours(0);
            date.setMinutes(0);
            date.setSeconds(0);
            mAlarm.setRepeating(AlarmManager.RTC_WAKEUP, date.getTime()
                    + configuer.getDndStart() * 60 * 60 * 1000,
                                (24 * 60 * 60 * 1000), startIntent);
            mAlarm.setRepeating(AlarmManager.RTC_WAKEUP, date.getTime()
                    + configuer.getDndEnd() * 60 * 60 * 1000,
                                (24 * 60 * 60 * 1000), cancleIntent);
        } else {
            mAlarm.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),
                       startIntent);
        }

    }

    @Override
    public void onStart() {
        super.onStart();
        // for network checking
        if (!NetworkCheck.isNetworkAvailable(this)) {
            Toast.makeText(this, R.string.nerwork_err_info, Toast.LENGTH_LONG)
                    .show();
            // showNetworkErrDialog();
        }
        Timer t = new Timer();
        t.schedule(new Task(), SPLASH_TIME);
    }

    /**
     * 无网络时显示Dialog，问用户是否设置网络，是则跳到系统网络设置页面
     * 
     * @deprecated 改为直接Toast显示，默认离线进入
     */
    public void showNetworkErrDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);

        DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                switch (which) {
                    case DialogInterface.BUTTON_POSITIVE:
                        startActivity(new Intent(
                                                 Settings.ACTION_WIRELESS_SETTINGS));
                        break;
                    case DialogInterface.BUTTON_NEUTRAL:
                        Intent intent = new Intent();
                        intent.setClass(SplashActivity.this,
                                        DeliveryComingActivity.class);
                        startActivity(intent);
                        finish();
                        break;
                    case DialogInterface.BUTTON_NEGATIVE:
                        finish();
                        break;
                }
            }
        };

        builder.setTitle(R.string.network_err_title)
                .setIcon(android.R.drawable.ic_dialog_info)
                .setPositiveButton(R.string.button_setting, listener)
                .setNegativeButton(R.string.button_exit, listener)
                .setNeutralButton(R.string.button_offline, listener)
                .setMessage(R.string.nerwork_err_info).create().show();
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }


	@Override
    protected void onPause() {
	    super.onPause();
	    MobclickAgent.onPause(this);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // Splash页面，禁用所有用户响应
        return false;
    }

    /**
     * The Class Task.
     */
    class Task extends TimerTask {

        /*
         * (non-Javadoc)
         * @see java.util.TimerTask#run()
         */
        @Override
        public void run() {
            Intent intent = new Intent();
            intent.setClass(SplashActivity.this, DeliveryComingActivity.class);
            startActivity(intent);
            finish();
        }
    }
}
